using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using AleaClasses;
using AleaPlayer;
using System.Threading;

namespace AleaDesigner
{
	/// <summary>
	/// Summary description for TfrmPlayer.
	/// </summary>
	public class TfrmPlayer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnPlay;
		private System.Windows.Forms.TextBox edMessages;
		private System.Windows.Forms.Button btnStop;
		private System.Windows.Forms.ListView lvDriveLabels;
		private System.Windows.Forms.ColumnHeader Labels;
		private System.ComponentModel.IContainer components;

		public TfrmPlayer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnPlay = new System.Windows.Forms.Button();
			this.edMessages = new System.Windows.Forms.TextBox();
			this.btnStop = new System.Windows.Forms.Button();
			this.lvDriveLabels = new System.Windows.Forms.ListView();
			this.Labels = new System.Windows.Forms.ColumnHeader();
			this.SuspendLayout();
			// 
			// btnPlay
			// 
			this.btnPlay.Location = new System.Drawing.Point(8, 8);
			this.btnPlay.Name = "btnPlay";
			this.btnPlay.Size = new System.Drawing.Size(56, 23);
			this.btnPlay.TabIndex = 0;
			this.btnPlay.Text = "Play";
			this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
			// 
			// edMessages
			// 
			this.edMessages.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.edMessages.Location = new System.Drawing.Point(136, 40);
			this.edMessages.Multiline = true;
			this.edMessages.Name = "edMessages";
			this.edMessages.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.edMessages.Size = new System.Drawing.Size(328, 324);
			this.edMessages.TabIndex = 1;
			this.edMessages.Text = "";
			// 
			// btnStop
			// 
			this.btnStop.Location = new System.Drawing.Point(72, 8);
			this.btnStop.Name = "btnStop";
			this.btnStop.Size = new System.Drawing.Size(56, 23);
			this.btnStop.TabIndex = 2;
			this.btnStop.Text = "Stop";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// lvDriveLabels
			// 
			this.lvDriveLabels.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							this.Labels});
			this.lvDriveLabels.Location = new System.Drawing.Point(8, 40);
			this.lvDriveLabels.Name = "lvDriveLabels";
			this.lvDriveLabels.Size = new System.Drawing.Size(121, 320);
			this.lvDriveLabels.TabIndex = 3;
			this.lvDriveLabels.View = System.Windows.Forms.View.Details;
			this.lvDriveLabels.SelectedIndexChanged += new System.EventHandler(this.lvDriveLabels_SelectedIndexChanged);
			// 
			// Labels
			// 
			this.Labels.Text = "Labels";
			this.Labels.Width = 112;
			// 
			// TfrmPlayer
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(480, 373);
			this.Controls.Add(this.lvDriveLabels);
			this.Controls.Add(this.btnStop);
			this.Controls.Add(this.edMessages);
			this.Controls.Add(this.btnPlay);
			this.Name = "TfrmPlayer";
			this.Text = "TfrmPlayer";
			this.ResumeLayout(false);

		}
		#endregion

		public TAleaGraph AleaGraph = null;
		TAleaPlayer AleaPlayer = null;
		bool fStop = false;
		ArrayList fMessages = new ArrayList();

		private void btnPlay_Click(object sender, System.EventArgs e)
		{
			// first, populate drive labels list
			lvDriveLabels.Items.Clear();
			foreach(TLabel lLabel in AleaGraph.Labels)
			{
				ListViewItem lvi = new ListViewItem();
				lvi.Text = lLabel.DisplayName;
				lvi.Tag = lLabel;
				lvDriveLabels.Items.Add(lvi);
			}

			Thread oThread = new Thread(new ThreadStart(DoPlay));

			oThread.Start();

			while (!oThread.Join( 500))
			{
				UpdateControlsDuringPlay();
				this.Refresh();
				Application.DoEvents();
			};
		}

		private void DoPlay()
		{
			AleaPlayer = new TAleaPlayer(AleaGraph);
			AleaPlayer.AleaPlayerMessageDelegate = new TAleaPlayerMessageDelegate(OnPlayerMessage);

			TNode aNode = null;
			fStop = false;

			aNode = AleaPlayer.Play(aNode);

			while (!fStop && aNode != null)
			{
				aNode = AleaPlayer.Play(aNode);
			}

			if (fStop)
				OnPlayerMessage("User aborted play");
			else
				OnPlayerMessage("Finished successfully");
		}

		private ArrayList GetDriveLabels()
		{
			ArrayList retval = new ArrayList();

			foreach (ListViewItem lvi in lvDriveLabels.SelectedItems)
			{
				if ( lvi.Tag is TLabel)
				{
					retval.Add((lvi.Tag as TLabel).LabelCode);
				}
			}

			return retval;
		}

		private void UpdateDriveLabels(ArrayList aDriveLabels)
		{
			foreach (ListViewItem lvi in lvDriveLabels.Items)
			{
				TLabel lLabel = (lvi.Tag as TLabel);

				if (lLabel != null)
				{
					lvi.Selected = aDriveLabels.Contains(lLabel.LabelCode);
				}
			}
		}

		public void OnPlayerMessage(string aMessage)
		{
			fMessages.Add(aMessage);
		}

		private void btnStop_Click(object sender, System.EventArgs e)
		{
			fStop = true;
		}

		private void UpdateControlsDuringPlay()
		{
			foreach(string lMessage in fMessages)
			{
				edMessages.Text += lMessage + Environment.NewLine;
			}
			fMessages.Clear();
			edMessages.SelectionStart = edMessages.Text.Length;
			edMessages.SelectionLength = 0;
			edMessages.ScrollToCaret();

			UpdateDriveLabels(AleaPlayer.DriveLabels);
		}

		private void timerPlayer_Tick(object sender, System.EventArgs e)
		{
		}

		private void lvDriveLabels_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			AleaPlayer.DriveLabels = GetDriveLabels();
		}
	}
}
